package org.jeecg.modules.demo.zc.idleCar.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.xcx.training.registration.entity.Registration;
import org.jeecg.modules.demo.zc.idleCar.entity.IdleCar;
import org.jeecg.modules.demo.zc.idleCar.service.IIdleCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 闲置车辆
 */
@Api(tags="idleCar")
@RestController
@RequestMapping("/zc/idleCar")
@Slf4j
public class IdleCarController extends JeecgController<IdleCar, IIdleCarService> {

    @Autowired
    private IIdleCarService idleCarService;

    /**
     * 分页列表查询
     *
     * @param idleCar
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "idleCar-分页列表查询")
    @ApiOperation(value="idleCar-分页列表查询", notes="idleCar-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<IdleCar>> queryPageList(IdleCar idleCar,
                                                    @RequestParam(name="orgCode", required = false) String orgCode,
                                                    @RequestParam(name="userid") String userid,
                                                     @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                     @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                     HttpServletRequest req) {

        if(orgCode == null){
            orgCode = "";
        }

        Integer orgId = idleCarService.getOrgId(orgCode);
        List<Integer> orgIdList = idleCarService.getparentOrgIds(orgId);
        orgIdList.add(orgId);

        QueryWrapper<IdleCar> queryWrapper = QueryGenerator.initQueryWrapper(idleCar, req.getParameterMap());
        queryWrapper.clear();
        queryWrapper.eq("IF_UNUSED", 1);
        queryWrapper.in("ORG_ID",orgIdList);
        Page<IdleCar> page = new Page<IdleCar>(pageNo, pageSize);
        IPage<IdleCar> pageList = idleCarService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

}
